package action.system;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import core.action.BaseAction;
import exception.Exceptions;
import model.security.UsrAuth;
import model.security.UsrRes;
import model.security.UsrRole;
import model.security.UsrUser;
import service.system.SystemListService;
import service.system.SystemUsraddService;
import util.Time;
import util.Tool;
import util.Validator;

public class SystemActionDo extends BaseAction{
	private SystemListService systemListService;
	private SystemUsraddService usraddService;
	
	private UsrUser user=new UsrUser();
	private UsrRole role=new UsrRole();
	private UsrRes  res=new UsrRes();
	private UsrAuth auth=new UsrAuth();
	
	private String password1;
	private String password2;
	
	private Long role_all_name;//从前台传入的用户权限值
	private String userNewRoles;
	
	private String addlist;
	private String dellist;
	
	private String aaa;
	
	//-----user------
	public String useredit() throws Exceptions{
		if(usercheck()){
			UsrUser us=systemListService.get_user(user.getUserid());
			this.user.setUseraccount(us.getUseraccount());
			this.user.setCreatetime(us.getCreatetime());
			
			//填写了密码，则进行修改密码
			if(password1.equals("")||password1==null){
				this.user.setUserpassword(us.getUserpassword());
				systemListService.updateUser(user,Tool.splitArrayHtml(userNewRoles));
			}
			else{
				if(passwordcheck()){
					//systemListService.updateUser(user,role_all_name);
					systemListService.updateUser(user,Tool.splitArrayHtml(userNewRoles));
				}
			}

			MessageJump("用户编辑成功","systemList!user");
		}
		return "message";
	}
	
	public String usersave(){
		if(usercheck()){
			if(passwordcheck()){
				user.setCreatetime(Time.CurrentTimestamp());
				String getStr=usraddService.addUser(user);
				if(getStr.equals("TRUE"))
					MessageJump("用户保存成功","systemList!user");
				else
					MessageJump(getStr,"BACK");
			}
		}
		return "message";
	}
	
	private boolean usercheck(){
		if(user.getUseraccount().isEmpty()){
			MessageJump("用户名不能为空","BACK");
			return false;
		}else if(user.getEmail().isEmpty()){
			MessageJump("邮箱不能为空","BACK");
			return false;
		}else if(!Validator.Email(user.getEmail())){
			MessageJump("邮箱格式不正确","BACK");
			return false;			
		}else{
			return true;
		}
	}
	
	private boolean passwordcheck(){
		if(password1.isEmpty()||password2.isEmpty()){
			MessageJump("密码不能为空","BACK");
			return false;
		}else{
			if(!password1.equals(password2)){
				MessageJump("密码不相同","BACK");
				return false;
			}else{
				password1=new Md5PasswordEncoder().encodePassword(password2,user.getUseraccount());
				user.setUserpassword(password1);
				return true;
			}
		}
	}
	
	//------role-------
	public String roleedit(){
		if(rolecheck()){
			systemListService.updateRole(role);
			
			if(addlist!=null&&addlist!=""&&!addlist.isEmpty()){
				systemListService.updateRoleAuthByAdd(role.getRoleid(),Tool.splitArrayHtml(addlist));
			}
			
/*			if(dellist!=null&&dellist!=""&&!dellist.isEmpty()){
			    systemListService.updateRoleAuthByDel(role.getRoleid(),Tool.splitArrayHtml(dellist));
			    
			}*/
			
			MessageJump("角色编辑成功","systemList!roleEdit?roleid="+role.getRoleid());	
		}
		return "message";
	}
	
	public String rolesave(){
		if(rolecheck()){
			role.setCreatetime(Time.CurrentTimestamp());
			if(usraddService.addRole(role)){
				MessageJump("角色保存成功","systemList!role");
			}
		}
		return "message";
	}
	
	private boolean rolecheck(){
		if(role.getRolename().isEmpty()||role.getEnabled()==null){
			MessageJump("资源名或资源连接不能为空","BACK");
			return false;
		}else{
			return true;
		}
	}
	
	//-----auth----
	public String authedit(){
		if(authcheck()){
			UsrAuth au=systemListService.get_auth(auth.getAuthid());
			this.auth.setCreatetime(au.getCreatetime());
			systemListService.updateAuth(auth);
			MessageJump("权限编辑成功","systemList!auth");
		}
		return "message";
	}
	
	public String authsave(){
		if(authcheck()){
			auth.setCreatetime(Time.CurrentTimestamp());
			if(usraddService.addAuth(auth)){
				MessageJump("权限保存成功","systemList!res");
			}
		}
		return "message";
	}
	
	private boolean authcheck(){
		if(auth.getAuthname().isEmpty()||auth.getEnabled()==null){
			MessageJump("资源名或资源连接不能为空","BACK");
			return false;
		}else{
			return true;
		}
	}
	
	//-------res-----
	public String resedit(){
		if(rescheck()){
			UsrRes rs=systemListService.get_res(res.getResid());
			this.res.setCreatetime(rs.getCreatetime());
			systemListService.updateRes(res);
			MessageJump("资源编辑成功","systemList!res");
		}
		return "message";
	}
		
	public String ressave(){
		if(rescheck()){
			res.setCreatetime(Time.CurrentTimestamp());
			usraddService.addRes(res);
			MessageJump("资源保存成功","systemList!res");
		}		
		return "message";
	}
	
	private boolean rescheck(){
		if(res.getReslink().isEmpty()||res.getResname().isEmpty()){
			MessageJump("资源名或资源连接不能为空","BACK");
			return false;
		}else{
			return true;
		}
	}
	//-----------------------------------
	public SystemListService getSystemListService() {
		return systemListService;
	}
	public void setSystemListService(SystemListService systemListService) {
		this.systemListService = systemListService;
	}

	public UsrRes getRes() {
		return res;
	}

	public void setRes(UsrRes res) {
		this.res = res;
	}

	public UsrUser getUser() {
		return user;
	}

	public void setUser(UsrUser user) {
		this.user = user;
	}

	public UsrRole getRole() {
		return role;
	}

	public void setRole(UsrRole role) {
		this.role = role;
	}

	public UsrAuth getAuth() {
		return auth;
	}

	public void setAuth(UsrAuth auth) {
		this.auth = auth;
	}


	public String getPassword1() {
		return password1;
	}


	public void setPassword1(String password1) {
		this.password1 = password1;
	}


	public String getPassword2() {
		return password2;
	}


	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public SystemUsraddService getUsraddService() {
		return usraddService;
	}

	public void setUsraddService(SystemUsraddService usraddService) {
		this.usraddService = usraddService;
	}

	public String getAaa() {
		return aaa;
	}

	public void setAaa(String aaa) {
		this.aaa = aaa;
	}

	public Long getRole_all_name() {
		return role_all_name;
	}

	public void setRole_all_name(Long role_all_name) {
		this.role_all_name = role_all_name;
	}

	public String getAddlist() {
		return addlist;
	}

	public void setAddlist(String addlist) {
		this.addlist = addlist;
	}

	public String getDellist() {
		return dellist;
	}

	public void setDellist(String dellist) {
		this.dellist = dellist;
	}

	public String getUserNewRoles() {
		return userNewRoles;
	}

	public void setUserNewRoles(String userNewRoles) {
		this.userNewRoles = userNewRoles;
	}	

}
